package com.example.joblinker.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.joblinker.common.Result;
import com.example.joblinker.dto.JobApplicationDTO;
import com.example.joblinker.service.JobApplicationService;
import com.example.joblinker.util.JwtUtil;
import com.example.joblinker.vo.JobApplicationVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@RequestMapping("/applications")
public class JobApplicationController {

    @Resource
    private JobApplicationService jobApplicationService;

    @Resource
    private JwtUtil jwtUtil;

    /**
     * 创建职位申请
     */
    @PostMapping
    public Result<JobApplicationVO> createApplication(@RequestBody @Valid JobApplicationDTO jobApplicationDTO, HttpServletRequest request) {
        Long userId = jwtUtil.getUserIdFromRequest(request);
        JobApplicationVO vo = jobApplicationService.createApplication(jobApplicationDTO, userId);
        return Result.success(vo);
    }

    /**
     * 获取职位申请详情
     */
    @GetMapping("/detail/{id}")
    public Result<JobApplicationVO> getApplicationDetail(@PathVariable Long id) {
        JobApplicationVO vo = jobApplicationService.getApplicationDetail(id);
        return Result.success(vo);
    }

    /**
     * 获取当前用户的申请列表
     */
    @GetMapping("/user-list")
    public Result<Page<JobApplicationVO>> getUserApplications(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            HttpServletRequest request) {
        Long userId = jwtUtil.getUserIdFromRequest(request);
        Page<JobApplicationVO> voPage = jobApplicationService.getUserApplications(userId, page, size);
        return Result.success(voPage);
    }

    /**
     * 获取指定职位的申请列表
     */
    @GetMapping("/job/{jobId}")
    public Result<Page<JobApplicationVO>> getJobApplications(
            @PathVariable Long jobId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<JobApplicationVO> voPage = jobApplicationService.getJobApplications(jobId, page, size);
        return Result.success(voPage);
    }

    /**
     * 更新申请状态
     */
    @PutMapping("/status-update/{id}")
    public Result<Void> updateApplicationStatus(
            @PathVariable Long id,
            @RequestParam Integer status,
            @RequestParam(required = false) String note) {
        jobApplicationService.updateApplicationStatus(id, status, note);
        return Result.success();
    }

    /**
     * 检查用户是否已申请职位
     */
    @GetMapping("/check")
    public Result<Boolean> hasApplied(
            @RequestParam Long jobId,
            HttpServletRequest request) {
        Long userId = jwtUtil.getUserIdFromRequest(request);
        boolean applied = jobApplicationService.hasApplied(jobId, userId);
        return Result.success(applied);
    }
}